package cmpe451.group7.ourcity.util;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.common.collect.Lists;

import cmpe451.group7.ourcity.entity.Tag;

/**
 * General utility class that contains common code.
 */
public class Utils {

	public static <T> boolean exactMatch(Collection<T> collection1,
			Collection<T> collection2) {
		return collection1.size() == collection2.size()
				&& collection1.containsAll(collection2);
	}

	public static boolean allZero(double... values) {
		for (int i = 0; i < values.length; i++) {
			if (values[i] != 0)
				return false;
		}
		return true;
	}

	public static boolean allZero(String... values) {
		for (int i = 0; i < values.length; i++) {
			if (StringUtils.isNotEmpty(values[i]))
				return false;
		}
		return true;
	}

	public static int parseIntFromMap(Map<String, String[]> keys, String key) {
		return Integer.parseInt(keys.get(key)[0]);
	}

	public static long parseLongFromMap(Map<String, String[]> keys, String key) {
		return Long.parseLong(keys.get(key)[0]);
	}

	public static boolean hasOneParameter(Map<String, String[]> parameters) {
		for (String[] values : parameters.values()) {
			if (values.length != 1) {
				return false;
			}
		}
		return true;
	}

	public static List<String> extractMIDs(List<Tag> tags) {
		List<String> transform = Lists.transform(tags, TagToMID.INSTANCE);
		transform.removeAll(Collections.singleton(null));
		return transform;
	}

	public static void reloadPage() throws IOException {
		ExternalContext ec = FacesContext.getCurrentInstance()
				.getExternalContext();
		ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
	}

	public static void redirectPage(String url) throws IOException {
		ExternalContext ec = FacesContext.getCurrentInstance()
				.getExternalContext();
		ec.redirect(url);
	}

	public static void addWarningMessage(String summary, String detail) {
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public static void addInfoMessage(String summary, String detail) {
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public static JSONObject sendWebServiceRequest(GenericUrl url)
			throws IOException, ParseException {

		HttpTransport httpTransport = new NetHttpTransport();
		HttpRequestFactory requestFactory = httpTransport
				.createRequestFactory();
		JSONParser parser = new JSONParser();

		// send the request
		HttpRequest request = requestFactory.buildGetRequest(url);
		HttpResponse httpResponse = request.execute();

		// parse the response as json object

		String httpString = httpResponse.parseAsString();
		JSONObject response = (JSONObject) parser.parse(httpString);

		return response;
	}

	public static JSONObject youtubeVideoProperties(String youtubeUrl) {
		GenericUrl url = new GenericUrl("https://www.youtube.com/oembed");
		url.put("url", youtubeUrl);
		url.put("format", "json");

		JSONObject response;
		try {
			response = Utils.sendWebServiceRequest(url);
			return response;
		} catch (IOException | ParseException e) {
			return null;
		}
	}
}
